package com.talking.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.talking.R;
import com.talking.bean.VertifyBean;
import com.talking.databinding.ItemVertityBinding;
import com.talking.inte.ParaClick;
import com.talking.model.Http;
import com.talking.presenter.UserPresenter;
import com.talking.utils.Utils;
import com.talking.view.VerityMsgActivity;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class VertifyAdapter extends ArrayAdapter<VertifyBean> {
    private static final int layoutId = R.layout.item_vertity;
    private final VerityMsgActivity context;
    private final ParaClick ignore,accept;
    public VertifyAdapter(VerityMsgActivity context, List<VertifyBean> objects, ParaClick ignore, ParaClick accept){
        super(context,layoutId,objects);
        this.context=context;
        this.ignore=ignore;
        this.accept=accept;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        VertifyBean vertifyBean = getItem(position);
        @SuppressLint("ViewHolder") @NonNull ItemVertityBinding binding = ItemVertityBinding.inflate(LayoutInflater.from(context));
        binding.itemVerityAccount.setText(vertifyBean.account);
        try {
            JSONObject content = new JSONObject(vertifyBean.content);
            binding.itemVerityGreet.setText(content.getString("greet"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        UserPresenter.getUserByAccount(vertifyBean.account,null, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                context.runOnUiThread(() -> {
                    try {
                        JSONObject sender = new JSONObject(response.body().string());
                        binding.itemVerityNickname.setText(sender.getString("nickname"));
                        Glide.with(context).load(Http.BASE_URL+ sender.getString("iconUrl")).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.itemVerityIcon);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
        binding.itemVerityIgnore.setOnClickListener(v -> ignore.onClick(vertifyBean));
        binding.itemVerityAccept.setOnClickListener(v->accept.onClick(vertifyBean));
        return binding.getRoot();
    }
}